
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 08/28/2010 00:19:11
-- Generated from EDMX file: C:\Users\bme\documents\visual studio 2010\Projects\MealPlanner\MealPlanner.DataModel\MealPlannerModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [MealPlanner];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_UserRecipe]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[RecipeSet] DROP CONSTRAINT [FK_UserRecipe];
GO
IF OBJECT_ID(N'[dbo].[FK_LanguageUser]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserSet] DROP CONSTRAINT [FK_LanguageUser];
GO
IF OBJECT_ID(N'[dbo].[FK_LanguageRecipe]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[RecipeSet] DROP CONSTRAINT [FK_LanguageRecipe];
GO
IF OBJECT_ID(N'[dbo].[FK_LanguageCountry]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CountrySet] DROP CONSTRAINT [FK_LanguageCountry];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[UserSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserSet];
GO
IF OBJECT_ID(N'[dbo].[RecipeSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[RecipeSet];
GO
IF OBJECT_ID(N'[dbo].[CountrySet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CountrySet];
GO
IF OBJECT_ID(N'[dbo].[LanguageSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[LanguageSet];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'UserSet'
CREATE TABLE [dbo].[UserSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [PasswordHash] nvarchar(max)  NOT NULL,
    [Email] nvarchar(max)  NOT NULL,
    [LanguageId] int  NOT NULL
);
GO

-- Creating table 'RecipeSet'
CREATE TABLE [dbo].[RecipeSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [LanguageId] int  NOT NULL,
    [PreparationTime] int  NOT NULL,
    [OwnerUserId] int  NOT NULL
);
GO

-- Creating table 'CountrySet'
CREATE TABLE [dbo].[CountrySet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Code] nvarchar(max)  NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [DefaultLanguageId] int  NOT NULL
);
GO

-- Creating table 'LanguageSet'
CREATE TABLE [dbo].[LanguageSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'AccountSet'
CREATE TABLE [dbo].[AccountSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [PasswordHash] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'LoginLogSet'
CREATE TABLE [dbo].[LoginLogSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [UserId] nvarchar(max)  NOT NULL,
    [TimeStamp] datetime  NOT NULL,
    [Failed] bit  NOT NULL,
    [User_Id] int  NOT NULL
);
GO

-- Creating table 'AccountSet_AdministratorAccount'
CREATE TABLE [dbo].[AccountSet_AdministratorAccount] (
    [Id] int  NOT NULL
);
GO

-- Creating table 'AccountSet_UserAccount'
CREATE TABLE [dbo].[AccountSet_UserAccount] (
    [Email] nvarchar(max)  NOT NULL,
    [Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'UserSet'
ALTER TABLE [dbo].[UserSet]
ADD CONSTRAINT [PK_UserSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'RecipeSet'
ALTER TABLE [dbo].[RecipeSet]
ADD CONSTRAINT [PK_RecipeSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id], [Code] in table 'CountrySet'
ALTER TABLE [dbo].[CountrySet]
ADD CONSTRAINT [PK_CountrySet]
    PRIMARY KEY CLUSTERED ([Id], [Code] ASC);
GO

-- Creating primary key on [Id] in table 'LanguageSet'
ALTER TABLE [dbo].[LanguageSet]
ADD CONSTRAINT [PK_LanguageSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'AccountSet'
ALTER TABLE [dbo].[AccountSet]
ADD CONSTRAINT [PK_AccountSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'LoginLogSet'
ALTER TABLE [dbo].[LoginLogSet]
ADD CONSTRAINT [PK_LoginLogSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'AccountSet_AdministratorAccount'
ALTER TABLE [dbo].[AccountSet_AdministratorAccount]
ADD CONSTRAINT [PK_AccountSet_AdministratorAccount]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'AccountSet_UserAccount'
ALTER TABLE [dbo].[AccountSet_UserAccount]
ADD CONSTRAINT [PK_AccountSet_UserAccount]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [OwnerUserId] in table 'RecipeSet'
ALTER TABLE [dbo].[RecipeSet]
ADD CONSTRAINT [FK_UserRecipe]
    FOREIGN KEY ([OwnerUserId])
    REFERENCES [dbo].[UserSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserRecipe'
CREATE INDEX [IX_FK_UserRecipe]
ON [dbo].[RecipeSet]
    ([OwnerUserId]);
GO

-- Creating foreign key on [LanguageId] in table 'UserSet'
ALTER TABLE [dbo].[UserSet]
ADD CONSTRAINT [FK_LanguageUser]
    FOREIGN KEY ([LanguageId])
    REFERENCES [dbo].[LanguageSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_LanguageUser'
CREATE INDEX [IX_FK_LanguageUser]
ON [dbo].[UserSet]
    ([LanguageId]);
GO

-- Creating foreign key on [LanguageId] in table 'RecipeSet'
ALTER TABLE [dbo].[RecipeSet]
ADD CONSTRAINT [FK_LanguageRecipe]
    FOREIGN KEY ([LanguageId])
    REFERENCES [dbo].[LanguageSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_LanguageRecipe'
CREATE INDEX [IX_FK_LanguageRecipe]
ON [dbo].[RecipeSet]
    ([LanguageId]);
GO

-- Creating foreign key on [DefaultLanguageId] in table 'CountrySet'
ALTER TABLE [dbo].[CountrySet]
ADD CONSTRAINT [FK_LanguageCountry]
    FOREIGN KEY ([DefaultLanguageId])
    REFERENCES [dbo].[LanguageSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_LanguageCountry'
CREATE INDEX [IX_FK_LanguageCountry]
ON [dbo].[CountrySet]
    ([DefaultLanguageId]);
GO

-- Creating foreign key on [User_Id] in table 'LoginLogSet'
ALTER TABLE [dbo].[LoginLogSet]
ADD CONSTRAINT [FK_UserLoginLog]
    FOREIGN KEY ([User_Id])
    REFERENCES [dbo].[UserSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserLoginLog'
CREATE INDEX [IX_FK_UserLoginLog]
ON [dbo].[LoginLogSet]
    ([User_Id]);
GO

-- Creating foreign key on [Id] in table 'AccountSet_AdministratorAccount'
ALTER TABLE [dbo].[AccountSet_AdministratorAccount]
ADD CONSTRAINT [FK_AdministratorAccount_inherits_Account]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[AccountSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'AccountSet_UserAccount'
ALTER TABLE [dbo].[AccountSet_UserAccount]
ADD CONSTRAINT [FK_UserAccount_inherits_Account]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[AccountSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------